document.ready(function() {
    webSpinner.spin(getItem('loadSpin'));
    listSpinner.spin(getItem('loading'));

    if (getStorage('housecraft_id') != null && getStorage('housecraft_id') != '') {
        //详情页返回加载
    	if (parseInt(getStorage('housecraft_page')) > 10) {
    		setStorage('housecraft_page', '10');
    	}
    	resetList();
    }
    else {
    	//首次进入加载
    	queryKey('ok');
    }
});

function resetList() {
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'housecraft';
    param.func = 'resetHousecraftList';
    param.sortation = encodeURIComponent(getStorage('housecraft_sortation'));
    param.keyword = encodeURIComponent(getStorage('housecraft_searchKeyword'));
    param.page = encodeURIComponent(getStorage('housecraft_page'));

    sendAjax(param, function(result) {
        var list = result.housecraftList;
        housecraft.list = list;

        setHtml('listCount', result.listCount);
        resetBottomStatus('reset', 19, list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });
}

function queryKey(flag) {
    resetPageBar(flag);
    resetBottomStatus('loading');

    var param = {};
    param.team = 'platform';
    param.ctrl = 'housecraft';
    param.func = 'getHousecraftList';
    param.sortation = getAjaxValue('sortation');
    param.keyword = getAjaxValue('searchKeyword');
    param.page = getAjaxValue('page');

    sendAjax(param, function(result) {
        var list = result.housecraftList;
        if (getValue('page') == '1' || housecraft.list == '') {
            housecraft.list = list;
            location.href = "#mainBg";
            setValue('page', '1');
        }
        else {
            if (list.length > 0) {
                for (var i = 0; i < list.length; i++) {
                    housecraft.list.push(list[i]);
                }
            }
        }

        setHtml('listCount', result.listCount);
        resetBottomStatus('bottomed', 19, list, housecraft.list);

        setDisplay('loadSpin', 'none');
        setDisplay('mainBg', '');
        webSpinner.spin();
    });

    if (flag == 'search') {
        submitSearchKeyword(getValue('searchKeyword'), 'housecraft');
    }
}

function slipsearch_clearOption() {
    //重置筛选值
    setValue('sortation', '');

    //重置筛选分类样式
    var sortation = '全部';
    var objList = getItem('optionTab').children;
    for (var i = 0; i < objList.length; i++) {
        if (sortation.indexOf(objList[i].innerHTML) != -1) {
            objList[i].className = 'tab-item-click';
        }
        else {
            objList[i].className = 'tab-item';
        }
    }
}

function viewHousecraft(id) {
    setStorage('housecraft_page', getValue('page'));
    setStorage('housecraft_id', id);
    setStorage('housecraft_sortation', getValue('sortation'));
    setStorage('housecraft_searchKeyword', getValue('searchKeyword'));

    toUrl('index.php?m=housecraft&c=info&from=list&housecraftid=' + id);
}

function selectSortation(obj, text, id) {
    if (obj.className == 'tab-item-click') {
        return;
    }

    var parentObj = obj.parentNode;
    var objList = parentObj.children;
    for (var i = 0; i < objList.length; i++) {
        objList[i].className = 'tab-item';
    }

    obj.className = 'tab-item-click';
    setValue(id, text);
    
    queryKey('ok');
}

function resetOption() {
    //重置筛选分类样式
    var sortation = getValue('sortation');
    if (sortation == '') {
        sortation = '全部';
    }
    else if (sortation == '0') {
        sortation = '月嫂保姆';
    }
    else if (sortation == '1') {
        sortation = '清洗保洁';
    }
    else if (sortation == '2') {
        sortation = '家居清洗';
    }

    var objList = getItem('optionTab').children;
    objList[0].className = 'tab-item';
    for (var i = 0; i < objList.length; i++) {
        if (sortation.indexOf(objList[i].innerHTML) != -1) {
            objList[i].className = 'tab-item-click';
            break;
        }
    }

    //当搜索框有值时，显示搜索框
    value = getValue('searchKeyword');
    if (value != '') {
        setDisplay('slipSearchBar', '');
    }
}